package Homework;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class TextEditorApp extends JFrame {

    // 文本框
    private JTextField filePathField;
    // 富文本框
    private JTextArea textArea;
    // 打开按钮
    private JButton openButton;
    // 保存按钮
    private JButton saveButton;
    // 复制按钮
    private JButton copyButton;

    public TextEditorApp() {
        // 设置窗口标题
        setTitle("文本处理器-Java文件流");
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口大小
        setSize(600, 400);
        // 设置窗口居中
        setLocationRelativeTo(null);
        // 设置布局管理器
        setLayout(new FlowLayout());

        // 设置初始文本和列宽
        filePathField = new JTextField("D:\\Test\\a.txt", 30);

        // 提供滚动功能
        textArea = new JTextArea(15, 50); // 设置行数和列数
        JScrollPane scrollPane = new JScrollPane(textArea); // 添加滚动条

        openButton = new JButton("打开");
        saveButton = new JButton("保存");
        copyButton = new JButton("复制");

        add(new JLabel("文件路径:"));
        add(filePathField);
        add(openButton);
        add(saveButton);
        add(copyButton);
        add(scrollPane);

        // 事件监听器
        // 打开按钮
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String filePath = filePathField.getText().trim();
                if (filePath.isEmpty()) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "请输入文件路径！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Path path = Paths.get(filePath);
                try {
                    if (Files.exists(path)) {
                        // 读取文件内容并设置到文本区域
                        String content = new String(Files.readAllBytes(path));
                        textArea.setText(content);
                    } else {
                        JOptionPane.showMessageDialog(TextEditorApp.this, "文件不存在: " + filePath, "错误", JOptionPane.WARNING_MESSAGE);
                        // 清空文本区域
                        textArea.setText("");
                    }
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "读取文件失败: " + ex.getMessage(), "IO 错误", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });

        // 保存按钮
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String filePath = filePathField.getText().trim();
                if (filePath.isEmpty()) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "请输入文件路径！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Path path = Paths.get(filePath);
                try {
                    // 将文本区域的内容写入文件
                    Files.write(path, textArea.getText().getBytes());
                    JOptionPane.showMessageDialog(TextEditorApp.this, "文件保存成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "保存文件失败: " + ex.getMessage(), "IO 错误", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });

        // 复制按钮
        copyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sourcePath = filePathField.getText().trim();
                String targetPath = "E:\\NewFolder\\t.abc";

                if (sourcePath.isEmpty()) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "源文件路径为空！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Path source = Paths.get(sourcePath);
                Path target = Paths.get(targetPath);

                try {
                    if (!Files.exists(source)) {
                        JOptionPane.showMessageDialog(TextEditorApp.this, "源文件不存在，无法复制: " + sourcePath, "错误", JOptionPane.WARNING_MESSAGE);
                        return;
                    }

                    // 确保目标目录存在
                    Files.createDirectories(target.getParent());

                    // 复制文件，StandardCopyOption.REPLACE_EXISTING 表示如果目标文件存在则覆盖
                    Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
                    JOptionPane.showMessageDialog(TextEditorApp.this, "文件已复制到: " + targetPath, "成功", JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(TextEditorApp.this, "复制文件失败: " + ex.getMessage(), "IO 错误", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
//                try {
//                    // 设置系统默认外观
//                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                // 创建并显示窗口
                new TextEditorApp().setVisible(true);
            }
        });
    }
}